<?php
class Line_bargaining_model extends CI_Model
{
    private $table   = 'line_bargaining';
    private $table_2 = 'allot_profit';
    private $table_3 = 'user';
    
    public function findByArray($arrayData)
    {
        if (empty($arrayData)) {
            return false;
        }
        $this->db->select('line_bargaining.*, user.user_name, user.alias_name');
        $this->db->from($this->table.' AS line_bargaining');
        $this->db->join($this->table_3.' AS user', 'line_bargaining.seller_uid = user.uid', 'inner');
        if (!empty($arrayData['bargaining_id'])) {
            $this->db->where('line_bargaining.bargaining_id', $arrayData['bargaining_id']);
        }
        if (!empty($arrayData['price_id'])) {
            $this->db->where('line_bargaining.price_id', $arrayData['price_id']);
        }
        if (!empty($arrayData['seller_uid'])) {
            $this->db->where_in('line_bargaining.seller_uid', $arrayData['seller_uid']);
        }
        return $this->db->get();
    }
    
    public function add($postData, $sellerUid)
    {
        $data =array(
            'provider_uid'            =>trim($postData['provider_uid']),
            'node'                    =>trim($postData['node']),
            'poundage'                =>trim($postData['poundage']) ? trim($postData['poundage']) : 0,
            'bankpay'                 =>trim($postData['bankpay'])  ? trim($postData['bankpay'])  : 0,
            'adult_original_price'    =>trim($postData['adult_original_price']),
            'adult_price'             =>trim($postData['adult_price']),
            'children_original_price' =>trim($postData['children_original_price']),
            'children_price'          =>trim($postData['children_price']),
            'seller_uid'              =>$sellerUid,
            'price_id'                =>trim($postData['price_id']),
            'home_price'              =>trim($postData['home_price']),
            'prosecond_id'            =>trim($postData['prosecond_id']) ? trim($postData['prosecond_id']) : 0,
            'prosecond_profit'        =>trim($postData['prosecond_profit']) ? trim($postData['prosecond_id']) : 0,
        );
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }
    
    public function update($postData){
        $data =array(
            'provider_uid'            =>trim($postData['provider_uid']),
            'node'                    =>trim($postData['node']),
            'poundage'                =>trim($postData['poundage']) ? trim($postData['poundage']) : 0,
            'bankpay'                 =>trim($postData['bankpay'])  ? trim($postData['bankpay'])  : 0,
            'adult_original_price'    =>trim($postData['adult_original_price']),
            'adult_price'             =>trim($postData['adult_price']),
            'children_original_price' =>trim($postData['children_original_price']),
            'children_price'          =>trim($postData['children_price']),
            'price_id'                =>trim($postData['price_id']),
            'home_price'              =>trim($postData['home_price']),
            'prosecond_id'            =>trim($postData['prosecond_id']) ? trim($postData['prosecond_id']) : 0,
            'prosecond_profit'        =>trim($postData['prosecond_profit']) ? trim($postData['prosecond_profit']) : 0,
        );
        $this->db->where('bargaining_id', trim($postData['bargaining_id']));
        return $this->db->update($this->table, $data);
    }
    
    public function deleteByBargainingId($bargaining_id)
    {
        $this->db->where('bargaining_id', $bargaining_id);
        return $this->db->delete($this->table);
    }
    
    public function validateBargainingSellerUid($price_id, $seller_uid)
    {
        $this->db->where('price_id', $price_id);
        $this->db->where('seller_uid', $seller_uid);
        if ($this->db->count_all_results($this->table) > 0) {
            return true;
        }
        return false;
    }
}